from PyQt5.QtCore import QAbstractListModel,QModelIndex,Qt
# from myglobal import MySet
import logging
class QuickModelBase(QAbstractListModel):
    def __str__(self):
        return "model:"+str(self.mDatas)
    def __init__(self,parent=None):
        super().__init__(parent)
        self.mDatas=[]
    def init_data(self,datas):
        self.mDatas+=datas
    def updateCalcInfo(self):
        pass
    def compareDataChanged(self, data1, data2):
        changed = False;

        set1=[]
        for it in data1:
            set1.append(it)
        set2=[]
        for it in data2:
            set2.append(it)

        for one in set1:
            if one in set2:
                pass
            else:
                changed=True
                break
        return changed;


    def rowCount(self, index=None):
        print(self.mDatas)
        return len(self.mDatas)
    def data(self, index, role):
        logging.info([index,role])
        if index.row() < 0 or index.row() >= len(self.mDatas):
            return {};

        if (role == Qt.DisplayRole or role == Qt.EditRole):
            data = self.mDatas[index.row()]
            logging.info(["data",data])
            return data
        return {}
    def data_row(self, row):
        return self.data(index(row), Qt.DisplayRole);

    def resetData(self, datas):
        # oldObjs = self.mAllDatas
        # self.mAllDatas.clear();
        self.beginResetModel();
        self.mDatas = datas;
        # it = datas.begin()
        # while(it != datas.end()):
        #     self.mAllDatas.insert(data.at(it))
        #     it+=1
        self.endResetModel();
        self.updateCalcInfo();

    def appendMany(self, datas):
        if len(datas) <= 0:
            return;
        self.beginInsertRows(QModelIndex(), len(self.mDatas), len(self.mDatas) + len(datas) - 1)
        for one in datas:
            self.mDatas.append(one)
        # it = datas.begin()
        # while(it != datas.end()):
        #     self.mAllDatas.insert(datas.at(it))
        #     it+=1
        self.endInsertRows();
        self.updateCalcInfo();
    def prepend(self, data):
        self.beginInsertRows(QModelIndex(), 0, 0);
        self.mDatas.insert(0,data);
        # self.mAllDatas.insert(data);
        self.endInsertRows();
        self.updateCalcInfo();
    def insert_many(self, row, datas):
        logging.info([row,datas])
        if row < 0 or row >= len(self.mDatas):
            return;
        self.beginInsertRows(QModelIndex(), row, row + len(datas) - 1);
        srow = row;
        for  obj in  datas:
            self.mDatas.insert(srow, obj);
            srow+=1
        self.endInsertRows();
        self.updateCalcInfo();
    def clear(self):
        mdatacount =len(self.mDatas)
        if (mdatacount == 0):
            return
        self.beginRemoveRows(QModelIndex(), 0, mdatacount - 1);
        self.mDatas=[]
        self.endRemoveRows();
        self.updateCalcInfo();
    def removeAt(self,row):
        if (row < 0 or row >= len(self.mDatas)):
            return;
        self.beginRemoveRows(QModelIndex(), row, row);
        obj = self.mDatas[row]
        del self.mDatas[row]
        self.endRemoveRows();
        self.updateCalcInfo();

    # def subtract(self, other):
    #     if len(other) == 0:
    #         return;
    #     self.beginResetModel();
    #     # self.mAllDatas.subtract(other);
    #     # self.mDatas = mAllDatas.values();
    #     self.endResetModel();
    #     self.updateCalcInfo();

    def updateData(self,row, data):
        if (row < 0 or row >= len(self.mDatas)):
            return;
        self.mDatas[row] = data
        self.dataChanged.emit(self.index(row, 0), self.index(row, 0))
        self.updateCalcInfo()
if __name__=="__main__":
    m=QuickModelBase()
    m.init_data([1,2,3])
    print(m)
    print(m.rowCount())
    m.appendMany([4,5,6])
    print("m",m)
    m.prepend(7)
    print(m)
    print("/////////////////////")
    m.insert_many(0, [9, 10])
    print(m)